/**
 * 
 */
package org.dse.dao;

import java.util.List;

import org.dse.beans.HtmlDocument;

/**
 * This class use Hibernate technology to implement additional methods the
 * HtmlDocumentDAO has.
 * 
 * @author zhangchen
 * 
 */
public class HtmlDocumentHibernateDAO extends
		GenericHibernateDAO<HtmlDocument, Integer> implements HtmlDocumentDAO {

	/**
	 * Use HQL to query an html document using its url.
	 * 
	 * @param url
	 *            the url
	 * @return the html document identified by the url
	 */
	@SuppressWarnings("unchecked")
	public HtmlDocument findByUrl(String url) {
		String queryString = "from HtmlDocument as d where d.docUrl = ?";
		List<HtmlDocument> documentList = getHibernateTemplate().find(
				queryString, url);

		if (documentList.size() == 0)
			return null;
		else
			return documentList.get(0);
	}

	/**
	 * Use SQL to get the max length of all documents.
	 * 
	 * @return an integer representing the max length
	 */
	public int getMaxLength() {
		String querySqlString = "select max(docLength) from htmldocument";

		return ((Integer) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createSQLQuery(querySqlString)
				.uniqueResult()).intValue();
	}
}
